package com.his.login.controller;

import com.his.config.annotation.CurrentUser;
import com.his.login.entity.DictDoctor;
import com.his.login.entity.DictEmployee;
import com.his.login.service.EmployeeService;
import com.his.login.vo.DictDoctorVo;
import com.his.login.vo.EmployeeVo;
import com.his.login.vo.PageEmployeeVo;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @Author xiaoxiaoping
 * @Date 2020/2/3 16:00
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/api/emp")
public class EmployeeController extends ResultBaseController {

    private  final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

    @Resource
    private EmployeeService employeeService;

    @RequestMapping(value = "/save.do")
    public ResultUtil saveEmployee(@CurrentUser DictEmployee dictEmployee,@Validated @RequestBody EmployeeVo employeeVo){
        try {
            employeeService.saveEmployee(dictEmployee,employeeVo);
            return this.success("保存成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @PostMapping(value="/update.do")
    public ResultUtil updateEmployee(@CurrentUser DictEmployee dictEmployee,@Validated @RequestBody EmployeeVo employeeVo){
        try {
            employeeService.updateEmployee(employeeVo);
            return this.success("修改成功", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping(value="/get_by_id.do")
    public ResultUtil getEmployyeById(@CurrentUser DictEmployee dictEmployee,String empId){
        try {
            return this.success("", employeeService.selectByEmpid(empId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**
     * 查询用户的公共方法
     * @param employeeVo
     * @return
     */
    @PostMapping(value ="/get_employee.do")
    public ResultUtil getEmployee(@CurrentUser DictEmployee dictEmployee,@RequestBody EmployeeVo employeeVo){
        try {
            return this.success("", employeeService.queryEmployeeByEntity(employeeVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**
     * 分页获取用户列表
     * @param dictEmployee
     * @param pageEmployeeVo
     * @return
     */
    @PostMapping(value ="/get_employee_by_page.do")
    public ResultUtil getEmployeeByPage(@CurrentUser DictEmployee dictEmployee,@RequestBody PageEmployeeVo pageEmployeeVo){
        try {
            return this.success("", employeeService.pageSelectEmployee(dictEmployee,pageEmployeeVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    /**
     *写入医生属性
     */
    @PostMapping(value ="/save_doctor.do")
    public ResultUtil insertDictDoctor(@CurrentUser DictEmployee dictEmployee,@RequestBody DictDoctorVo dictDoctorVo){
        try {
            employeeService.insertDictDoctor(dictDoctorVo);
            return this.success("保存成功！", null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**
     *
     * @param dictEmployee
     * @param empId
     * @return
     */
    @GetMapping(value ="/get_doctor.do")
    public ResultUtil getDictDoctor(@CurrentUser DictEmployee dictEmployee,String empId){
        try {
            return this.success("", employeeService.selectByPrimaryKey(empId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

    /**
     * 获取用户基本信息和菜单
     * @param dictEmployee
     * @return
     */
    @GetMapping(value="/get_info.do")
    public ResultUtil getEmpInfo(@CurrentUser DictEmployee dictEmployee){
        try {
            return this.success("",employeeService.getEmpInfo(dictEmployee));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

}
